class MyPage extends BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        super(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl);
    }
    OnPageLoad() {
        // 回车直接进行搜索
        $("panel_search").bind('keyup', function (e) {
            var key = e.keyCode;
            if (key === 13) {
                _Instance.Fetch();
            }
        });
        $('#loading').hide();
    }
    SetCriterion(item) {
        item['StyleNo'] = $.trim($("#StyleNo").val());
        return item;
    }
    GetFilter() {
        let item = super.GetFilter(0);
        // 设置条件
        item = this.SetCriterion(item);
        return JSON.stringify(item);
    }
    EmptyTableContent() {
        $("#tab_data01 tbody").empty();
    }
    SetTableContent(result) {
        $("#tab_data01 tbody").empty();

        let _totalQuantity = 0;
        let _poMaterialTotalAmount = 0;
        let _poNoMaterialTotalAmount = 0;
        let _payabledTotalAmount = 0;

        let content_01 = '';
        for (let i = 0; i < result.length; i++) {
            // rowFlag 0-正常行 1-非材料花费 2-附加费
            if (parseInt(result[i].rowFlag) === 0) {
                let _tmpVal = (result[i].poQuantity === null ? 0 : result[i].poQuantity);
                if (_tmpVal === 0) {
                    content_01 += '<tr class="row__error">';
                } else {
                    content_01 += '<tr>';
                }
                content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialCode) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].specification) + '</td>';
                content_01 += '<td style="text-align:center;;">' + formatNullData(result[i].uomName) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].materialColourName) + '</td>';
                content_01 += '<td style="text-align:right;">' + result[i].factoryPieceYardage + '</td>';
                content_01 += '<td style="text-align:right;">' + (result[i].unitPrice === 0 ? '' : result[i].unitPrice) + '</td>';
                content_01 += '<td style="text-align:right;">' + (result[i].stockLevels === 0 ? '' : result[i].stockLevels) + '</td>';
                content_01 += '<td style="text-align:right;">' + (result[i].mrpQuantity === 0 ? '' : result[i].mrpQuantity) + '</td>';           // 调达数
                content_01 += '<td style="text-align:right;">' + (result[i].poQuantity === 0 ? '' : result[i].poQuantity) + '</td>';             // 采购数
                _totalQuantity += result[i].poQuantity;
                _poMaterialTotalAmount += result[i].payabledAmount;
                _payabledTotalAmount += result[i].payabledAmount;
                content_01 += '<td style="text-align:right;">' + (result[i].poAmount === 0 ? '' : result[i].poAmount) + '</td>';                  // 采购金额
                content_01 += '<td style="text-align:right;">' + (result[i].payabledQuantity === 0 ? '' : result[i].payabledQuantity) + '</td>';  // 付款数
                content_01 += '<td style="text-align:right;">' + (result[i].payabledAmount === 0 ? '' : result[i].payabledAmount) + '</td>';      // 付款金额
                content_01 += '<td style="text-align:right;">' + (result[i].diffQuantity === 0 ? '' : result[i].diffQuantity) + '</td>';          // 数量差值
                content_01 += '<td style="text-align:right;">' + (result[i].diffAmount === 0 ? '' : result[i].diffAmount) + '</td>';              // 金额差值
                content_01 += '</tr>';
            }
            if (parseInt(result[i].rowFlag) === 1) {
                content_01 += '<tr>';
                content_01 += '<td>' + formatNullData(result[i].styleNo) + '</td>';
                content_01 += '<td>' + formatNullData(result[i].supplierName) + '</td>';
                content_01 += '<td colspan="10">' + formatNullData(result[i].materialName) + '</td>';
                content_01 += '<td style="text-align:right;">' + (result[i].poAmount === 0 ? '' : result[i].poAmount) + '</td>';
                content_01 += '<td></td>';
                content_01 += '<td style="text-align:right;">' + (result[i].payabledAmount === 0 ? '' : result[i].payabledAmount) + '</td>';
                content_01 += '<td colspan="2"></td>';
                content_01 += '</tr>';
                _poNoMaterialTotalAmount += result[i].payabledAmount;
                _payabledTotalAmount += result[i].payabledAmount;   // 付款总金额
            }
            if (parseInt(result[i].rowFlag) === 2) {
                content_01 += '<tr style="background-color:#ffe4b5; color:#000;">';
                content_01 += '<td colspan="12" style="text-align: right; font-weight: bold;">' + '（小    计）' + '</td>';
                content_01 += '<td style="text-align:right; font-weight: bold;">' + (result[i].poAmount === 0 ? '' : result[i].poAmount) + '</td>';                  // 采购金额
                content_01 += '<td></td>';
                content_01 += '<td style="text-align:right; font-weight: bold;">' + (result[i].payabledAmount === 0 ? '' : result[i].payabledAmount) + '</td>';      // 付款金额
                content_01 += '<td colspan="2"></td>';
                content_01 += '</tr>';
            }
            $("#tab_data01 tbody").append(content_01);
            content_01 = '';
        }
        $("#PoTotalAmount").text(formatMoney(_poMaterialTotalAmount.toFixed(2), 1));
        $("#PoNoMaterialTotalAmount").text(formatMoney(_poNoMaterialTotalAmount.toFixed(2), 1));
        $("#PayabledTotalAmount").text(formatMoney(_payabledTotalAmount.toFixed(2), 1));
        this.rows = result.length;
        setOneTableRowColor("tab_data01");

        this.AutoAdjustTableHeightForLines();   // 自适应高度设置
        $(window).resize();                     // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    //《按钮》搜索
    DoSearch() {
        this.FetchForPage(1);
    }
    AutoAdjustTableHeightForLines() {
        let h = $(window).height();
        let _rc = Math.floor((h - 147 - 80) / 25);
        let _dh = _rc * 25;
        let _tah = this.rows * 25;
        _dh = (_tah >= _dh) ? _dh : _tah;

        $("#div_content").css({"max-height": _dh, height: _dh});
    }
    ExportToExcel(table, sheetname, filename) {
        let uri = 'data:application/vnd.ms-excel;base64,',
                template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>',
                base64 = function (s) {
                    return window.btoa(unescape(encodeURIComponent(s)));
                },
                format = function (s, c) {
                    return s.replace(/{(\w+)}/g,
                            function (m, p) {
                                return c[p];
                            });
                };
        if (!table.nodeType)
            table = document.getElementById(table);
        let ctx = {worksheet: sheetname || 'Worksheet', table: document.getElementById("tab_head01").innerHTML + table.innerHTML};
        document.getElementById("dlink").href = uri + base64(format(template, ctx));
        document.getElementById("dlink").download = filename; // 这里是关键所在,当点击之后,设置a标签的属性,这样就可以更改标签的标题了
        document.getElementById("dlink").click();
    }
}

var _Instance = null;

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    $("#search_btn").hide(); // 无需搜索，隐藏搜索按钮
    $("#StyleNo").bigAutocomplete({
        url: parent.basePath + "/master/product/fetchSerialNoForAutoComplate.html"
    });

    _Instance = new MyPage(0, null, null, null, null, null, null, null,
            "/financial/report/style_cost/fetch.html", null);

    _Instance.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    if (_Instance !== null)
        _Instance.AutoAdjustTableHeightForLines();
}
